package com.cq.hd.app.controller;

import com.cq.hd.app.biz.AuthBiz;
import com.cq.hd.app.vo.LoginVo;
import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.entity.wx.dto.WxLoginMobileDto;
import com.cq.hd.common.entity.wx.vo.Code2SessionVo;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.member.api.dto.MobileLoginDto;
import com.cq.hd.member.api.dto.LoginDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/auth")
@Api(tags = "auth相关接口")
public class AuthController {

    @Autowired
    private AuthBiz authBiz;

    // 根据code，获取openId和sessionKey，根据openId判断用户是否已经注册，如果已注册，则返回token，否则返回openId
    @ApiOperation(value = "wxCode登录校验")
    @ApiResponse(response = Code2SessionVo.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/code")
    public Result<Code2SessionVo> code(String code, HttpServletRequest request) {
        return ResultGenerator.success(authBiz.code2session(code, request));
    }

    // 根据openId+手机号加密数据，解密
    @ApiOperation(value = "获取并解密wx手机号")
    @ApiResponse(response = String.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/getWxMobile")
    public Result<String> getWxMobile(@RequestBody WxLoginMobileDto wxLoginMobileDto) {
        return ResultGenerator.success(authBiz.getWxMobile(wxLoginMobileDto));
    }

    // 根据openId+头像+昵称，注册成功，返回token
    @ApiOperation(value = "微信注册")
    @ApiImplicitParam(name = "loginDto", value = "手机号登录参数", required = true, paramType = "body", dataType = "LoginDto")
    @ApiResponse(response = LoginVo.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto, HttpServletRequest request) {
        return ResultGenerator.success(authBiz.login(loginDto, request));
    }

    @ApiOperation(value = "用户名密码登录")
    @ApiImplicitParam(name = "mobileLoginDto", value = "手机号登录参数", required = true, paramType = "body", dataType = "MobileLoginDto")
    @ApiResponse(response = LoginVo.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/loginByMobile")
    public Result<LoginVo> loginByMobile(@RequestBody MobileLoginDto mobileLoginDto, HttpServletRequest request) {
        return ResultGenerator.success(authBiz.loginByMobile(mobileLoginDto, request));
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public Result<Boolean> logout(@ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        return ResultGenerator.success(authBiz.logout(appLoginUser));
    }

//    @ApiOperation(value = "根据userId获取token（测试用）")
//    @ApiResponse(response = LoginVo.class, code = 200, message = "接口返回对象参数")
//    @PostMapping("/getToken")
//    public Result<LoginVo> getToken(Long userId, HttpServletRequest request) {
//        return ResultGenerator.success(authBiz.getToken(userId, request));
//    }

}
